package com.jh.complicate.serializable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Administrator
 * @projectName newforward
 * @title FileSerializer.java
 * @package com.jh.complicate.serializable
 * @description 文件序列化
 * @date 2019/6/4 23:06
 */
public class FileSerializer implements ISerializable{

    @Override
    public <T> byte[] serialize(T obj) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("userObj.txt")));
            oos.writeObject(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new byte[0];
    }

    @Override
    public <T> T deserialize(byte[] data, Class clazz) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("userObj.txt")));
            return (T) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }
}
